package com.buaa.aimall.goodsModule.Enum;

import com.buaa.aimall.goodsModule.Entity.GoodsOrderEntity;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.function.Consumer;

@Getter
@AllArgsConstructor
public enum GoodsOrderSortTypeEnum {
    CREATE_TIME_ASC("create_time_asc", wrapper -> wrapper.orderByAsc(GoodsOrderEntity::getCreateTime)),
    CREATE_TIME_DESC("create_time_desc", wrapper -> wrapper.orderByDesc(GoodsOrderEntity::getCreateTime)),
    AMOUNT_ASC("amount_asc", wrapper -> wrapper.orderByAsc(GoodsOrderEntity::getAmount)),
    AMOUNT_DESC("amount_desc", wrapper -> wrapper.orderByDesc(GoodsOrderEntity::getAmount)),
    DEFAULT("default", wrapper -> wrapper.orderByDesc(GoodsOrderEntity::getCreateTime));

    private final String code;
    private final Consumer<MPJLambdaWrapper<GoodsOrderEntity>> sortFunction;

    public static GoodsOrderSortTypeEnum fromCode(String code) {
        if (code == null) {
            return DEFAULT;
        }
        for (GoodsOrderSortTypeEnum type : values()) {
            if (type.getCode().equals(code)) {
                return type;
            }
        }
        return DEFAULT;
    }
}
